package com.my.chapter10;

public class practice22 {
    public static void main(String[] args) {
        Sequence22.main(args);
    }
}

interface Selector22{
    boolean end();
    Object current();
    void next();
}

class Sequence22{
    private Object[] items;
    private int next = 0;
    public Sequence22(int size){
        items = new Object[size];
    }
    public void add(Object x){
        if(next<items.length){
            items[next++] = x;
        }
    }
    private class SequenceSelector implements Selector22{
        private int i = 0;

        @Override
        public boolean end() {
            return i==items.length;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if(i<items.length){
                i++;
            }
        }

        public Sequence22 outer(){
            return Sequence22.this;
        }
    }

    private class ReverseSelector implements Selector22{
        private int i = items.length-1;

        @Override
        public boolean end() {
            return i==-1;
        }

        @Override
        public Object current() {
            return items[i];
        }

        @Override
        public void next() {
            if(i>-1){
                i--;
            }
        }

        public Sequence22 outer(){
            return Sequence22.this;
        }
    }

    public SequenceSelector sequenceSelector(){
        return new SequenceSelector();
    }

    public ReverseSelector reverseSelector(){ return new ReverseSelector(); }

    public static void main(String[] args) {
        Sequence22 sequence = new Sequence22(10);
        SequenceSelector selector1 = sequence.sequenceSelector();
        for(int i=0;i<10;i++){
            selector1.outer().add(Integer.toString(i));
        }

        while(!selector1.end()){
            System.out.print(selector1.current() + " ");
            selector1.next();
        }

        ReverseSelector selector2 = sequence.reverseSelector();

        System.out.println("");

        while(!selector2.end()){
            System.out.print(selector2.current() + " ");
            selector2.next();
        }

    }
}
